package com.wufun.overseas.main.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.papa.controller.core.PadKeyEvent;
import com.wufun.overseas.R;
import com.wufun.overseas.joystick.map.BlackListManager;
import com.wufun.overseas.joystick.map.HandShankUtil;
import com.wufun.overseas.joystick.map.KeyMap;
import com.wufun.overseas.joystick.map.KeyMapManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by caowd on 2015/6/12.
 */
public class HandShankSetKeyActivity extends HandShankBaseActivity implements View.OnClickListener {

    private Map<String, KeyInfo> data;
    private Map<String, String>  keyMap;

    @Bind(R.id.l2)
    Button l2;
    @Bind(R.id.l1)
    Button l1;
    @Bind(R.id.r2)
    Button r2;
    @Bind(R.id.r1)
    Button r1;

    @Bind(R.id.up)
    Button up;
    @Bind(R.id.left)
    Button left;
    @Bind(R.id.right)
    Button right;
    @Bind(R.id.down)
    Button down;

    @Bind(R.id.select)
    Button select;
    @Bind(R.id.start)
    Button start;


    @Bind(R.id.x)
    Button x;
    @Bind(R.id.y)
    Button y;
    @Bind(R.id.a)
    Button a;
    @Bind(R.id.b)
    Button b;

    //判断是否是从无蓝牙记录页进入
//    @Extra
    String from;

    private Button selectBtn;
//    private boolean isReset;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handshank_set_key);
        ButterKnife.bind(this);

        if (getIntent() != null)
            from = getIntent().getStringExtra("from");

        afterViews();

    }

    private List<String> unSetKeyCode;
    private List<String> unSetKeyTag;

    private void unSet() {
        unSetKeyCode = new ArrayList<String>();
        unSetKeyTag = new ArrayList<String>();
        for (String tag : HandShankUtil.map.keySet()) {
            if (keyMap.containsValue(tag)) {
                continue;
            }
            unSetKeyTag.add(tag);
        }

        for (Integer keyCode : HandShankUtil.map.values()) {
            if (keyMap.containsKey(keyCode + "")) {
                continue;
            }
            unSetKeyCode.add(keyCode + "");
        }

        Iterator iterator = unSetKeyTag.iterator();
        while (iterator.hasNext()) {
            String  tag          = (String) iterator.next();
            Integer keyBoradName = HandShankUtil.map.get(tag);
            if (unSetKeyCode.contains(keyBoradName + "")) {
                iterator.remove();
                unSetKeyCode.remove(keyBoradName + "");
                keyMap.put(keyBoradName + "", tag);
            }
        }

        if (unSetKeyCode.size() == unSetKeyTag.size()) {
            for (int i = 0; i < unSetKeyCode.size(); i++) {
                keyMap.put(unSetKeyCode.get(i), unSetKeyTag.get(i));
            }
        }


    }

    @OnClick(R.id.okButn)
    void okButn() {

        /*if(isReset) {
            isReset = false;
            Intent intent = new Intent(HandShankSetKeyActivity.this,HandShankYesActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);

            startActivity(intent);
            finish();

        }else {

            if (keyMap.size() < 14) {
                Toast.makeText(this, "全部配置后才能提交哦", Toast.LENGTH_SHORT).show();
            } else {
                keyMap.put(107 + "", HandShankUtil.KEY_BUTTON_THUMBR);
                keyMap.put(106 + "", HandShankUtil.KEY_BUTTON_THUMBL);
                KeyMapManager.writeToConfig(KeyMap.keySection, keyMap);


                Intent intent = new Intent(HandShankSetKeyActivity.this, HandShankYesActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                startActivity(intent);
                finish();
            }
        }*/


        if (keyMap.size() < 14) {
            unSet();
        }


        keyMap.put(107 + "", HandShankUtil.KEY_BUTTON_THUMBR);
        keyMap.put(106 + "", HandShankUtil.KEY_BUTTON_THUMBL);
        KeyMapManager.writeToConfigVK(KeyMap.keySection, keyMap);
        Intent intent = new Intent(HandShankSetKeyActivity.this, HandShankYesActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        startActivity(intent);
        finish();


    }

    @OnClick(R.id.resetButn)
    void resetButn() {
        // revertBtn();
        clear();
    }

    @OnClick(R.id.backImage)
    void backImage() {
        if (TextUtils.isEmpty(from)) {
            finish();
        } else {
            if (from.equals("first")) {
                Intent intent = new Intent(this, HandShankYesActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
                finish();
            }
        }
    }

    //    @AfterViews
    void afterViews() {
        initMap();
        initBtn();


    }

    private void clear() {

        Set      set = data.keySet();
        Iterator i   = set.iterator();
        while (i.hasNext()) {
            KeyInfo info = data.get(i.next());

            Button button = (Button) findViewById(info.id);
            button.setBackgroundResource(info.normal_drawable);
            if (info.normal_txt_color != 0) {
                button.setTextColor(getResources().getColor(info.normal_txt_color));
            }
        }

        keyMap.clear();
        keyMap.put(108 + "", HandShankUtil.KEY_START);
        keyMap.put(109 + "", HandShankUtil.KEY_SELECT);
        keyMap.put(99 + "", HandShankUtil.KEY_X);
        keyMap.put(96 + "", HandShankUtil.KEY_A);
        keyMap.put(100 + "", HandShankUtil.KEY_Y);
        keyMap.put(97 + "", HandShankUtil.KEY_B);
        keyMap.put(102 + "", HandShankUtil.KEY_L1);
        keyMap.put(104 + "", HandShankUtil.KEY_L2);
        keyMap.put(103 + "", HandShankUtil.KEY_R1);
        keyMap.put(105 + "", HandShankUtil.KEY_R2);

        keyMap.put(21 + "", HandShankUtil.KEY_DPAD_LEFT);
        keyMap.put(19 + "", HandShankUtil.KEY_DPAD_UP);
        keyMap.put(22 + "", HandShankUtil.KEY_DPAD_RIGHT);
        keyMap.put(20 + "", HandShankUtil.KEY_DPAD_DOWN);


        keyMap.put(107 + "", HandShankUtil.KEY_BUTTON_THUMBR);

        keyMap.put(106 + "", HandShankUtil.KEY_BUTTON_THUMBL);
        KeyMapManager.writeToConfigVK(KeyMap.keySection, keyMap);
        keyMap.clear();


        Toast.makeText(HandShankSetKeyActivity.this, getString(R.string.gamepad_keymap_success), Toast.LENGTH_LONG).show();


    }

    private void initMap() {
        data = new HashMap<String, KeyInfo>();
        data.put(HandShankUtil.KEY_L1, new KeyInfo(R.id.l1, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));
        data.put(HandShankUtil.KEY_L2, new KeyInfo(R.id.l2, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));
        data.put(HandShankUtil.KEY_R1, new KeyInfo(R.id.r1, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));
        data.put(HandShankUtil.KEY_R2, new KeyInfo(R.id.r2, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));


        data.put(HandShankUtil.KEY_Y, new KeyInfo(R.id.y, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));
        data.put(HandShankUtil.KEY_X, new KeyInfo(R.id.x, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));
        data.put(HandShankUtil.KEY_A, new KeyInfo(R.id.a, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));
        data.put(HandShankUtil.KEY_B, new KeyInfo(R.id.b, R.drawable.handshank_set_key_normal, R.drawable.handshank_set_key_selected, R.drawable.handshank_set_key_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));


        data.put(HandShankUtil.KEY_SELECT, new KeyInfo(R.id.select, R.drawable.handshank_set_skey_normal, R.drawable.handshank_set_skey_selected, R.drawable.handshank_set_skey_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));
        data.put(HandShankUtil.KEY_START, new KeyInfo(R.id.start, R.drawable.handshank_set_skey_normal, R.drawable.handshank_set_skey_selected, R.drawable.handshank_set_skey_pressed, R.color.handshank_key_set_normal_txt, R.color.handshank_key_set_selected_txt, R.color.handshank_key_set_pressed_txt));

        data.put(HandShankUtil.KEY_DPAD_UP, new KeyInfo(R.id.up, R.drawable.handshank_set_top_normal, R.drawable.handshank_set_top_selected, R.drawable.handshank_set_top_pressed, 0, 0, 0));
        data.put(HandShankUtil.KEY_DPAD_DOWN, new KeyInfo(R.id.down, R.drawable.handshank_set_down_normal, R.drawable.handshank_set_down_selected, R.drawable.handshank_set_down_pressed, 0, 0, 0));
        data.put(HandShankUtil.KEY_DPAD_LEFT, new KeyInfo(R.id.left, R.drawable.handshank_set_left_normal, R.drawable.handshank_set_left_selected, R.drawable.handshank_set_left_pressed, 0, 0, 0));
        data.put(HandShankUtil.KEY_DPAD_RIGHT, new KeyInfo(R.id.right, R.drawable.handshank_set_right_normal, R.drawable.handshank_set_right_selected, R.drawable.handshank_set_right_pressed, 0, 0, 0));


        keyMap = new HashMap<String, String>();
    }

    private void initBtn() {
        l2.setOnClickListener(this);
        l1.setOnClickListener(this);
        r2.setOnClickListener(this);
        r1.setOnClickListener(this);

        x.setOnClickListener(this);
        y.setOnClickListener(this);
        a.setOnClickListener(this);
        b.setOnClickListener(this);

        up.setOnClickListener(this);
        down.setOnClickListener(this);
        left.setOnClickListener(this);
        right.setOnClickListener(this);

        start.setOnClickListener(this);
        select.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //获取button的tag值
        String tag = (String) view.getTag();
        //keyMap没有tag时，进入流程
        if (!keyMap.containsValue(tag)) {
            //之前有选中的selectBtn
            if (selectBtn != null) {
                //选中的selectBtn跟之前一样，不处理
                if (tag.equals(selectBtn.getTag())) {
                    return;
                }
                //取出选中的selectBtn的keyInfo信息,设为正常
                KeyInfo keyInfo = data.get(selectBtn.getTag());
                selectBtn.setBackgroundResource(keyInfo.normal_drawable);
                if (keyInfo.normal_txt_color != 0) {
                    selectBtn.setTextColor(getResources().getColor(R.color.handshank_key_set_normal_txt));
                }
            }
            //重置selectBtn
            selectBtn = (Button) view;
            //取出当前selectBtn的信息
            KeyInfo keyInfo = data.get(selectBtn.getTag());
            //设置为选中状态
            selectBtn.setBackgroundResource(keyInfo.select_drawable);
            if (keyInfo.normal_txt_color != 0) {
                selectBtn.setTextColor(getResources().getColor(R.color.handshank_key_set_selected_txt));
            }


        }


    }


    private void revertBtn() {
        Set      set = data.entrySet();
        Iterator i   = set.iterator();
        while (i.hasNext()) {
            Map.Entry<String, KeyInfo> entry1 = (Map.Entry<String, KeyInfo>) i.next();
            String                     key    = entry1.getKey();
            KeyInfo                    info   = data.get(key);
            //取出所有的btn的信息值
            //如果包含tag值
            if (keyMap.containsValue(key)) {
                //设为选中
                Button button = (Button) findViewById(info.id);
                if (info.normal_txt_color != 0) {
                    button.setTextColor(getResources().getColor(info.pressed_txt_color));
                }
                button.setBackgroundResource(info.pressed_drawable);
            } else {
                //设为不选中
                Button button = (Button) findViewById(info.id);
                if (info.normal_txt_color != 0) {
                    button.setTextColor(getResources().getColor(info.normal_txt_color));
                }
                button.setBackgroundResource(info.normal_drawable);
            }


        }


    }

    @Override
    protected boolean onKeyEvent(int keyCode, PadKeyEvent event) {
        String deviceName = event.getName();
        if (BlackListManager.getInstance().isInBlackList(deviceName)) return false;

        if (event.getAction() == KeyEvent.ACTION_DOWN) {
//            onKeyDown1(keyCode, padEvent2KeyEvent(event));
        } else {
            onKeyUp1(keyCode, padEvent2KeyEvent(event));
        }

        return super.onKeyEvent(keyCode, event);
    }

    public boolean onKeyUp1(int keyCode, KeyEvent event) {
        String deviceName = event.getDevice().getName();
        if (BlackListManager.getInstance().isInBlackList(deviceName)) return false;

        if (keyCode != KeyEvent.KEYCODE_BACK) {

            if (keyCode == KeyEvent.KEYCODE_BUTTON_THUMBL || keyCode == KeyEvent.KEYCODE_BUTTON_THUMBR) {
                return true;
            }

            //存在选中的selectBtn,进入流程
            if (selectBtn != null) {
                //取出selectBtn的tag
                String tag = (String) selectBtn.getTag();
                //存入对应的tag与keyCode
                keyMap.put(keyCode + "", tag);
                //isReset = false;
                //取出该tag的btn的keyInfo信息值
                KeyInfo keyInfo = data.get(selectBtn.getTag());
                //设为选中状态
                selectBtn.setBackgroundResource(keyInfo.pressed_drawable);
                selectBtn.setTextColor(getResources().getColor(R.color.handshank_key_set_pressed_txt));
                selectBtn = null;
                revertBtn();
            }


            return true;
        }
        return super.onKeyUp(keyCode, event);

    }


    private class KeyInfo {
        public int id;
        public int normal_drawable;
        public int select_drawable;
        public int pressed_drawable;
        public int normal_txt_color;
        public int select_txt_color;
        public int pressed_txt_color;

        public KeyInfo(int id, int normal_drawable, int select_drawable, int pressed_drawable, int normal_txt_color, int select_txt_color, int pressed_txt_color) {
            this.id = id;
            this.normal_drawable = normal_drawable;
            this.select_drawable = select_drawable;
            this.pressed_drawable = pressed_drawable;
            this.normal_txt_color = normal_txt_color;
            this.select_txt_color = select_txt_color;
            this.pressed_txt_color = pressed_txt_color;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {

        super.onPause();
    }

}
